<?php
Class WorkFlowConverter {
	Public static function ToFrontEnd($backendSrcSting) {

		$backend = json_decode($backendSrcSting, true);
		$arrNodes = array();
		$arrAdditionalDataOne = array();
		$arrAdditionalDataTwo = array();
		$arrMainValues = array();
		$arrConnections = array();
		foreach ($backend['nodes'] as $node) {
			$arrMainValues["name"] = $node['additionalData']["name"];
			$arrMainValues["id"] = $node['additionalData']["id"];
			$arrMainValues["x"] = $node['additionalData']["x"];
			$arrMainValues["y"] = $node['additionalData']["y"];
			$arrMainValues["width"] = $node['additionalData']["width"];
			$arrMainValues["inputConnectors"] = $node['additionalData']["inputConnectors"];
			$arrMainValues["outputConnectors"] = $node['additionalData']["outputConnectors"];
			$arrAdditionalDataTwo["description"] = $node['description'];
			$arrAdditionalDataTwo["nodeType"] = $node['nodeType'];
			$arrAdditionalDataTwo["endpoints"] = $node['endpoints'];
			$arrAdditionalDataTwo["collector"] = $node['collector'];
			$arrAdditionalDataTwo["importContract"] = $node['importContract'];
			$arrAdditionalDataTwo["exportContract"] = $node['exportContract'];
			$arrMainValues["additionalData"] = $arrAdditionalDataTwo;
			$arrAdditionalDataOne[] = $arrMainValues;
			unset($arrAdditionalDataTwo);
		}
		foreach ($backend['connections'] as $connections) {
			$arrConnections[] = $connections;
		}

		$arrNodes['nodes'] = $arrAdditionalDataOne;
		$arrNodes['connections'] = $arrConnections;
		
		return json_encode($arrNodes);
		//echo json_encode($arrNodes);
	}

	// Frontend json to backend json create class
	Public static function ToBackEnd($frontendSrcSting) {

		$frontend = json_decode($frontendSrcSting, true);		
		// Gettting first node name and start node name
		$processName = "Personal";
		$startNode = $frontend['nodes']['0']['name'];
		$arrProcess = array(name => $processName, startNode => $startNode);
		// Other Subarray used
		$arrNodes = array();
		$arrDataprocessor = array();
		$arradditionalData = array();
		$arrOthersAdditionalData = array();
		$arrConnections = array();
		foreach ($frontend['nodes'] as $node) {// Main node values were stored here
			$arrDataprocessor['name'] = $node['name'];
			if ($node['additionalData']['description'] != null) {
				$arrDataprocessor['description'] = $node['additionalData']['description'];
			}
			if ($node['additionalData']['nodeType'] != null) {
				$arrDataprocessor['nodeType'] = $node['additionalData']['nodeType'];
			}
			if ($node['additionalData']['collector'] != null) {
				$arrDataprocessor['collector'] = $node['additionalData']['collector'];
			}
			if ($node['additionalData']['endpoints'] != null) {
				$arrDataprocessor['endpoints'] = $node['additionalData']['endpoints'];
			}
			if ($node['additionalData']['dataProcessor'] != null) {
				$arrDataprocessor['dataProcessor'] = $node['additionalData']['dataProcessor'];
			}
			if ($node['additionalData']['importContract'] != null) {
				$arrDataprocessor['importContract'] = $node['additionalData']['importContract'];
			}
			if ($node['additionalData']['exportContract'] != null) {
				$arrDataprocessor['exportContract'] = $node['additionalData']['exportContract'];
			}
			$arradditionalData['name'] = $node['name'];
			$arradditionalData['id'] = $node['id'];
			$arradditionalData['x'] = $node['x'];
			$arradditionalData['y'] = $node['y'];
			$arradditionalData['width'] = $node['width'];
			if ($node['inputConnectors'] != null) {
				$arradditionalData['inputConnectors'] = $node['inputConnectors'];
			}
			if ($node['outputConnectors'] != null) {
				$arradditionalData['outputConnectors'] = $node['outputConnectors'];
			}
			$arrDataprocessor['additionalData'] = $arradditionalData;
			$arrNodes[] = $arrDataprocessor;
			unset($arrDataprocessor);
			unset($arradditionalData);

		}
		foreach ($frontend['connections'] as $connections) {// Getting all node connections
			$arrConnections[] = $connections;
		}
		// Merging node values to main array
		$arrProcess['nodes'] = $arrNodes;
		// Merging connections values to main array
		$arrProcess['connections'] = $arrConnections;

		return json_encode($arrProcess);
        //echo json_encode($arrProcess);
	}

}
?>
